import { Fight } from '../Entity/def/Fight'
export class MapConfig {

    floors: any[] = []

    constructor() {
        floors.forEach(floor => {
            this.floors.push(JSON.parse(JSON.stringify(floor)))
        })
    }

    loadEntities(floor: number): EntityConfig[] {
        const floorConfig = this.floors[floor]
        const configs = []
        for (const entityTypeKey in floorConfig) {
            if (entityTypeKey === 'hero') {
                continue
            }
            if (Object.prototype.hasOwnProperty.call(floorConfig, entityTypeKey)) {
                const entityType = floorConfig[entityTypeKey];
                const resources = config[entityTypeKey]
                for (const subEntityType in entityType) {
                    if (Object.prototype.hasOwnProperty.call(entityType, subEntityType)) {
                        const indexs = entityType[subEntityType];
                        if (entityTypeKey === 'monster') {
                            let res = resources[subEntityType]
                            if (!res) {
                                res = 'enemies/' + subEntityType
                            }
                            configs.push(new EntityConfig(entityTypeKey + '-' + subEntityType, res, indexs, this.buildFight(monster[subEntityType])))
                            continue
                        }
                        if (entityTypeKey === 'multi') {
                            configs.push(new EntityConfig(subEntityType, undefined, indexs))
                            continue
                        }
                        configs.push(new EntityConfig(entityTypeKey + '-' + subEntityType, resources[subEntityType], indexs))
                    }
                }
            }
        }
        return configs
    }

    private buildFight(resource: any): Fight {
        if (!resource) {
            return undefined
        }
        return {
            life: resource.life,
            defense: resource.defense,
            magicAtk: resource.magicAtk,
            atk: resource.atk,
            money: resource.money,
            exper: resource.exper
        }
    }

    loadHero(floor: number, state?: string): string {
        if (state !== 'up' && state !== 'down') {
            state = 'up'
        }
        const floorConfig = this.floors[floor]
        if (Object.prototype.hasOwnProperty.call(floorConfig, 'hero')) {
            const heroIndex = floorConfig['hero']
            if (Object.prototype.hasOwnProperty.call(heroIndex, state)) {
                return heroIndex[state][0]
            }
        }
        return null
    }
}

export class EntityConfig {
    type: string
    resource: string
    indexs: string[]
    fight: Fight

    constructor(type: string, resource: string, indexs: [], fight?: Fight) {
        this.type = type
        this.resource = resource
        this.indexs = indexs
        this.fight = fight
    }
}

const floors = [
    {
        'walls': {
            'wall1': ["3-0", "7-0", "7-1", "7-2", "7-3", "6-3", "3-3", "3-2", "3-1", "4-3", "4-4", "4-5", "6-4", "6-5", "6-6", "6-7", "6-8", "6-9", "6-10", "4-10", "4-9", "4-8", "4-7", "4-6"]
        },
        'doors': {
            'yellow': ["5-3"],
            'red': []
        },
        'monster': {},
        'keys': {
            'yellow': [],
            'red': [],
            'blue': [],
        },
        'bloodBottle': {
            'blue': [],
            'red': []
        },
        'abilityStone': {
            'atk': [],
            'defense': []
        },
        'stairway': {
            'up': ['5-10'],
        },
        'hero': {
            'down': ['5-9'],
            'up': ['5-1']
        },
        'npc': {
            'angel': ['4-2']
        }
    },
    {
        'walls': {
            'wall1': ['0-9', '1-9', '2-9', '3-9', '4-9', '5-9', '6-9', '7-9', '8-9', '9-9',
                '5-8', '9-8',
                '3-7', '5-7', '9-7',
                '0-6', '2-6', '3-6', '5-6', '6-6', '7-6', '9-6',
                '3-5', '9-5',
                '3-4', '5-4', '6-4', '7-4', '8-4', '9-4',
                '0-3', '2-3', '3-3',
                '3-2', '4-2', '6-2', '7-2', '8-2', '10-2',
                '3-1', '7-1',
                '3-0', '7-0',
            ]
        },
        'monster': {
            '1-1': ['3-10', '5-10', '7-5'],
            '1-2': ['4-10'],
            '1-3': ['8-6'],
            '2-1': ['1-7', '2-8'],
            '2-2': ['1-2', '1-5'],
            '3-1': ['6-5'],
            '4-1': ['9-1'],
            '5-1': ['8-5'],
        },
        'doors': {
            'yellow': ['1-3', '1-6', '3-8', '5-5', '9-2'],
            'red': ['5-2']
        },
        'keys': {
            'yellow': ['2-0', '2-1', '0-5', '0-7', '8-0', '9-0', '10-0', '8-1', '7-8', '7-7', '2-10'],
            'red': ['4-1'],
            'blue': ['2-4', '10-1'],
        },
        'bloodBottle': {
            'blue': ['1-1'],
            'red': ['0-0', '0-1', '8-8', '8-7', '6-8', '6-7', '0-8']
        },
        'abilityStone': {
            'atk': ['2-7'],
            'defense': ['0-4']
        },
        'stairway': {
            'up': ['0-10'],
        },
        'hero': {
            'down': ['1-10'],
            'up': ['5-1']
        }
    },
    {
        'walls': {
            'wall1': ["1-9", "1-10", "1-8", "1-7", "1-6", "1-5", "1-3", "1-2", "1-1", "1-0", "3-3", "3-2", "3-1", "3-0", "4-3", "4-5", "3-5", "3-9", "3-8", "3-7", "3-6", "5-10", "5-9", "5-8", "5-7", "6-7", "7-7", "8-7", "10-10", "10-9", "10-8", "10-7", "10-6", "10-5", "10-4", "10-3", "10-2", "10-1", "10-0", "8-3", "8-2", "8-1", "8-0", "6-5", "6-3", "6-4", "6-2", "6-1", "6-0", "7-5", "9-5"]
        },
        'doors': {
            'yellow': ["2-5", "2-3", "5-5", "7-6", "8-5", "9-7"],
            'blue': ['5-3'],
            'green': ['1-4']
        },
        'monster': {
            '6-2': ['3-10'],
            '6-1': ["8-4", "9-8"]
        },
        'keys': {
            'yellow': ["2-2", "2-1", "2-7", "2-8", "4-8", "4-7", "8-9", "8-10", "8-8"],
            'red': ["9-10"],
            'blue': ["9-9"],
        },
        'bloodBottle': {
            'blue': ["4-2", "4-1", "4-0", "4-9"],
            'red': ["5-2", "5-1", "5-0"]
        },
        'abilityStone': {
            'atk': ["2-0", "6-10", "6-9", "6-8"],
            'defense': ["7-10", "7-9", "7-8", "2-9"]
        },
        'stairway': {
            'up': ['0-0'],
            'down': ['0-10'],
        },
        'hero': {
            'down': ['0-1'],
            'up': ['0-9']
        }
    },
    {
        'walls': {
            'wall1': ["3-10", "7-10", "8-10", "9-10", "10-10", "3-9", "7-9", "3-8", "4-8", "6-8", "7-8", "0-7", "2-7", "3-7", "3-6", "4-6", "5-6", "7-7", "7-6", "7-5", "9-7", "9-8", "9-6", "9-5", "9-4", "9-3", "9-2", "9-1", "9-0", "8-3", "6-3", "5-4", "5-3", "5-2", "5-1", "5-0", "4-0", "3-0", "2-0", "3-2", "2-2", "1-2", "0-2", "0-1", "1-5", "1-4", "2-4", "3-4", "4-4"],
        },
        'doors': {
            'yellow': ["1-7", "5-8", "7-3"],
            'red': []
        },
        'monster': {
            '1-2': ["1-10", "0-9", "4-5", "6-2", "8-2", "10-7", "10-5"],
            '1-1': ["0-5", "0-4"],
            '2-1': ["1-8", "5-7"],
            '5-1': ["3-5", "5-5", "4-2", "7-1", "9-9"]
        },
        'keys': {
            'yellow': ["2-10", "1-9", "0-8", "8-7", "8-6", "8-5", "8-1", "8-0"],
            'red': [],
            'blue': [],
        },
        'bloodBottle': {
            'blue': ['7-0'],
            'red': []
        },
        'abilityStone': {
            'atk': ["6-0"],
            'defense': ["6-1"]
        },
        'stairway': {
            'up': ['10-0'],
            'down': ['0-0'],
        },
        'hero': {
            'down': ['10-1'],
            'up': ['1-0']
        },
        'multi': {
            'store': ['4-10', '5-10', '6-10']
        }
    },
    {
        'walls': {
            'wall1': ["1-9", "1-8", "1-7", "1-6", "1-5", "1-4", "1-3", "1-2", "1-1", "1-0", "3-1", "3-2", "4-2", "3-3", "3-4", "3-5", "3-6", "3-7", "3-8", "3-9", "3-10", "4-8", "6-8", "7-8", "7-9", "7-10", "7-7", "7-6", "7-5", "6-5", "4-5", "7-4", "7-3", "7-2", "6-2", "7-1", "9-9", "9-8", "9-7", "9-6", "9-5", "9-4", "9-3", "9-2", "9-1", "9-0"],
        },
        'doors': {
            'yellow': ["0-9", "2-9", "8-9", "10-9"],
            'blue': ["5-2"],
            'red': ["5-5"]
        },
        'monster': {
            '1-2': ["0-4", "10-4"],
            '1-3': ["1-10", "9-10", "3-0", "7-0"],
            '2-1': ["2-7", "8-7"],
            '4-1': ["4-4", "5-3", "6-4"],
            '5-1': ["0-6", "0-5", "10-6", "10-5"],
            '5-2': ["4-7", "5-6", "6-7"],
            '5-3': ["5-7"],
            '7-1': ["5-4"]
        },
        'keys': {
            'yellow': ["4-1", "6-1"],
            'red': [],
            'blue': [],
        },
        'bloodBottle': {
            'blue': [],
            'red': ["2-6", "2-5", "8-6", "8-5"]
        },
        'abilityStone': {
            'atk': ["4-3", "6-3"],
            'defense': ["4-6", "6-6"]
        },
        'stairway': {
            'up': ['0-0'],
            'down': ['10-0'],
        },
        'hero': {
            'down': ['0-1'],
            'up': ['10-1']
        },
        'npc': {
            'angel': ["5-10"]
        }
    },
]
const config = {
    'walls': {
        'wall1': 'walls/wall1'
    },
    'stairway': {
        'down': 'steps/down',
        'up': 'steps/up'
    },
    'doors': {
        'yellow': 'doorandkeys/door1',
        'blue': 'doorandkeys/door2',
        'red': 'doorandkeys/door3',
        'green': 'doorandkeys/door4'
    },
    'keys': {
        'yellow': 'doorandkeys/key1',
        'blue': 'doorandkeys/key2',
        'red': 'doorandkeys/key3',
        'green': 'doorandkeys/key4'
    },
    'abilityStone': {
        'atk': 'ability_stone/red_stone',
        'defense': 'ability_stone/blue_stone',
    },
    'bloodBottle': {
        'red': 'blood_bottle/red_bottle',
        'blue': 'blood_bottle/blue_bottle',
    },
    'monster': {

    },
    'npc': {
        'angel': 'npc/angel'
    },
}

export const store = {
    'level': {
        'label': '提升等级（100经验）',
        'exper': -100,
        'gold': 0,
        'level': 1,
        'life': 200,
        'defense': 10,
        'atk': 10,
        'magicDefense': 10
    },
    'atk': {
        'label': '增加攻击力（15金币）',
        'gold': -15,
        'atk': 15
    },
    'life': {
        'label': '增加生命（15金币）',
        'gold': -15,
        'life': 200,
    },
    'defense': {
        'label': '增加防御（15金币）',
        'gold': -15,
        'defense': 10,
    },
    'magicDefense': {
        'label': '增加魔抗（15金币）',
        'gold': -15,
        'magicDefense': 10
    },
    'cancel': {
        'label': '离开',
    },
}

const hero = {
    'state': {
        'level': 1,
        'money': 0,
        'exper': 0,
    },
    'fight': {
        'life': 200,
        'defense': 20,
        'magicDefense': 20,
        'atk': 20,
    },
    'keys': {
        'yellow': 0,
        'blue': 0,
        'red': 0,
        'green': 0
    },
}

const monster = {
    '1-1': {
        'life': 20,
        'defense': 10,
        'magicAtk': 0,
        'atk': 10,
        'money': 10,
        'exper': 10
    },
    '1-2': {
        'life': 20,
        'defense': 10,
        'magicAtk': 0,
        'atk': 10,
        'money': 10,
        'exper': 10
    },
    '1-3': {
        'life': 40,
        'defense': 10,
        'magicAtk': 0,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '2-1': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '2-2': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '3-1': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 0,
        'money': 20,
        'exper': 20
    },
    '4-1': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '5-1': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '5-2': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '6-1': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
    '6-2': {
        'life': 40,
        'defense': 10,
        'magicAtk': 30,
        'atk': 30,
        'money': 20,
        'exper': 20
    },
}
